package com.hook3.pianke.view;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hook3.pianke.R;
import com.hook3.pianke.domain.BaseBean;
import com.hook3.pianke.domain.TalkInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2015/12/12.
 */
public class CommunityBaseDataHuaTi {
    public Activity activity;
    public String url;
    public String sort;
    public View view;

    public ListView listView;
    public JSON oJson;

    public CommunityBaseDataHuaTi(Activity activity, String url, String sort) {
        this.activity = activity;
        this.url = url;
        this.sort = sort;


//        initView();
//
//        initData();
    }

    public void initView() {
        view = View.inflate(activity, R.layout.listview_community_common, null);

        listView = (ListView) view.findViewById(R.id.lv_community_listview_common);

    }


    public void initData() {
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("sort", sort);
        params.addBodyParameter("start", "0");
        params.addBodyParameter("client", "2");
        params.addBodyParameter("limit", "10");
        x.http().post(params, new Callback.CacheCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                oJson =gson.fromJson(result, JSON.class);


                listView.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return oJson.data.list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return oJson.data.list.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder viewHolder = new ViewHolder();
                        if (convertView == null) {

                            convertView = View.inflate(activity, R.layout.item_listview_community, null);

                            viewHolder.iv_jinghua = (ImageView) convertView.findViewById(R.id.iv_jinghua);
                            viewHolder.iv_zhiding = (ImageView) convertView.findViewById(R.id.iv_zhiding);
                            viewHolder.iv_item_comment_icon = (ImageView) convertView.findViewById(R.id.iv_item_comment_icon);
                            viewHolder.tv_item_comment_title = (TextView) convertView.findViewById(R.id.tv_item_comment_title);
                            viewHolder.tv_item_comment_content = (TextView) convertView.findViewById(R.id.tv_item_comment_content);
                            viewHolder.tv_publish_time = (TextView) convertView.findViewById(R.id.tv_publish_time);
                            viewHolder.tv_publish_comment = (TextView) convertView.findViewById(R.id.tv_publish_comment);
                            convertView.setTag(viewHolder);
                        } else {
                            viewHolder = (ViewHolder) convertView.getTag();
                        }

                        x.image().bind(viewHolder.iv_item_comment_icon,oJson.data.list.get(position).coverimg);
                        viewHolder.tv_item_comment_title.setText(oJson.data.list.get(position).title);
                        viewHolder.tv_item_comment_content.setText(oJson.data.list.get(position).content);
                        viewHolder.tv_publish_time.setText(oJson.data.list.get(position).addtime_f);
                        viewHolder.tv_publish_comment.setText(oJson.data.list.get(position).counterList.comment);

                        return convertView;
                    }

                    class ViewHolder {
                        public ImageView iv_jinghua;
                        public ImageView iv_zhiding;
                        public ImageView iv_item_comment_icon;
                        public TextView tv_item_comment_title;
                        public TextView tv_item_comment_content;
                        public TextView tv_publish_time;
                        public TextView tv_publish_comment;
                    }
                });


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }


    public class JSON extends BaseBean {
        public Data data;
        public String result;
    }

    public class Data{
        public List<ListCoDetails> list;
        public String total;
    }
    public class ListCoDetails{
        public String addtime;
        public String addtime_f;
        public String content;
        public String contentid;
        public CounterList counterList;
        public String coverimg;
        public String ishot;
        public String isrecommend;
        public String songid;
        public String title;
        public Userinfo userinfo;
    }

    public class CounterList{
        public String comment;
        public String like;
        public String view;
    }
    public class Userinfo{
        public String icon;
        public String uid;
        public String uname;
    }
}
